package com.supermap.business.product;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.supermap.dataservice.util.JSONUtils;
import com.supermap.dubbo.api.product.ExpertConsultationService;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.util.UUID;

/**
 * @Author:zhoujian
 * @Desciption:专家会商
 * @date:2019-07-24 16:02
 * @version:v1.0.0
 */
@RestController
@RequestMapping("consultation")
public class ExperConsultationController {

    @Reference(timeout = 100000)
    ExpertConsultationService expertConsultationService;

    /**
     * 添加会商记录
     *
     * @param jsonObject
     * @return
     */
    @PostMapping("add")
    public JSONUtils insert(@RequestBody JSONObject jsonObject) {
        return expertConsultationService.insertConsultation(jsonObject);
    }

    /**
     * 邀请参会人员
     *
     * @param id
     * @param jsonUserIds
     * @param joinUserNames
     * @return
     */
    @GetMapping("addJoinUser")
    public JSONUtils addJoinUser(@RequestParam("id") Integer id, @RequestParam("joinUserIds") String jsonUserIds, @RequestParam("joinUserNames") String joinUserNames) {
        return expertConsultationService.addJoinUser(id, jsonUserIds, joinUserNames);
    }

    /**
     * 移除某一参会人员
     * @param id
     * @param initiateUserId
     * @param joinUserId
     * @param joinUserName
     * @return
     */
    @GetMapping("delJoinUser")
    public JSONUtils delJoinUser(@RequestParam("id") Integer id, @RequestParam("initiateUserId") String initiateUserId , @RequestParam("joinUserId") String joinUserId, @RequestParam("joinUserName") String joinUserName)
    {
        return  expertConsultationService.delJoinUser(id  , initiateUserId , joinUserId , joinUserName);
    }

    /**
     * 查看历史聊天记录
     * @param id
     * @return
     */
    @GetMapping("historyChat")
    public JSONUtils selectHistoryChat(@RequestParam("id") Integer id)
    {
        return  expertConsultationService.selectHistoryChat(id);
    }

    /**
     * 根据userId查询参与的会商记录
     * @param userId
     * @return
     */
    @GetMapping("selectListByUserId")
    public JSONUtils selectListByUserId(@RequestParam("userId")String userId)
    {
        return  expertConsultationService.selectConsultationListByJsonUserId(userId);
    }

    /**
     * 根据id查询会商详情
     * @param id 会商ID
     * @return
     */
    @GetMapping("details")
    public JSONUtils selectByPrimaryKey(@RequestParam("id") Integer id)
    {
        return expertConsultationService.selectByPrimaryKey(id);
    }

    /**
     * 上传会商文件
     * @param file 上传文件
     * @param uploadUserName 上传文件用户名
     * @param consultationId 上传文件的会商id
     * @return
     */
    @PostMapping("upload")
    public JSONUtils uploadFile(@RequestParam("file") MultipartFile file ,
                                @RequestParam("uploadUserName") String uploadUserName ,
                                @RequestParam("consultationId")Integer consultationId)
                                throws Exception
    {
        if (file.isEmpty()) {
            return new JSONUtils().setIsSuccess(false).setMsg("上传文件不能为空");
        }
        String fileName = file.getOriginalFilename();
        String filePath = "/mnt/ChinaApp/ApacheDir/html/epp/file/";
        String uuidFileName = UUID.randomUUID().toString()+fileName.substring(fileName.lastIndexOf("."),fileName.length());

        File dest = new File(filePath + uuidFileName);
        try {
            file.transferTo(dest);
        }catch (Exception ex)
        {
            ex.printStackTrace();;
            return new JSONUtils().setMsg("上传文件异常").setIsSuccess(false);
        }
        String fileUrl = "http://cqpisa.cn/epp/file/" + uuidFileName;
        return  expertConsultationService.uploadFile(fileUrl, fileName, uploadUserName , consultationId);
    }

    /**
     *  根据会商Id查询会商文件
     * @param consultationId 会商Id
     * @return
     */
    @GetMapping("selectFileList")
    public JSONUtils selectConsultationFileByConsultationId(@Param("consultationId") Integer consultationId)
    {
        return  expertConsultationService.selectFileListByConsultation(consultationId);
    }

}